package cn.yitun.chat.yitunserverimcore.general.main.biz.contact.manager;

import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactRelationDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactRelationVo;
import cn.yitun.chat.yitunservercommonlib.enums.contact.ContactBlockStatusEnums;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactRelationDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-27 08:45:57<br>
 * Description
 * 
 * @author yitun<br>
 * @since 1.1
 */
@Service
public class ContactRelationManager {

	@Resource
	private ContactRelationDao contactRelationDAO;

	/**
	 * 
	 * Date 2019-01-20 17:44:16<br>
	 * Description 相互添加为联系人
	 * 
	 * @author yitun<br>
	 * @param applyUserId
	 * @param applyCategoryId
	 * @param applyRemark
	 * @param targetUserId
	 * @param targetCategoryId
	 * @param targetRemark
	 * @return boolean
	 * @version 1.1
	 */
	public boolean bothContact(
			Long applyUserId,
			Long applyCategoryId,
			String applyRemark,
			Long targetUserId,
			Long targetCategoryId,
			String targetRemark) {
		boolean mark = true;
		if (!hasContact(applyUserId, targetUserId)) {
			add(applyUserId, targetUserId, applyCategoryId, applyRemark);
		}
		if (!hasContact(targetUserId, applyUserId)) {
			add(targetUserId, applyUserId, targetCategoryId, targetRemark);
		}
		return mark;
	}

	/**
	 * 
	 * Description 新增 <br>
	 * Date 2019-05-08 23:20:55<br>
	 * 
	 * @param ownerUserId
	 * @param contactUserId
	 * @param categoryId
	 * @param remark
	 * @return ContactRelation
	 * @version 1.1
	 */
	public ContactRelationDto add(
			Long ownerUserId, // 拥有者id
			Long contactUserId, // 成员id
			Long categoryId, // 分组id
			String remark) {
		String isBlocked = ContactBlockStatusEnums.BLOCK_NO.getType();
		ContactRelationDto bean = new ContactRelationDto();
		bean.setOwnerUserId(ownerUserId);
		bean.setContactUserId(contactUserId);
		bean.setCategoryId(categoryId);
		bean.setRemark(remark);
		bean.setIsBlocked(isBlocked);
		bean.setCreateBy(ownerUserId);
		bean.setUpdateBy(ownerUserId);
		contactRelationDAO.save(bean);
		return bean;
	}

	/**
	 * 
	 * Description 是否在黑名单中 <br>
	 * Date 2019-05-08 23:20:34<br>
	 * 
	 * @param ownerUserId
	 * @param contactUserId
	 * @return boolean
	 * @version 1.1
	 */
	public boolean isBlocked(Long ownerUserId, Long contactUserId) {
		ContactRelationVo bean = contactRelationDAO.getVOByContactUserId(ownerUserId, contactUserId);
		boolean isBlocked = null != bean && ContactBlockStatusEnums.BLOCK_YES.getType().equals(bean.getIsBlocked());
		return isBlocked;
	}

	/**
	 * 
	 * Description 是否存在 <br>
	 * Date 2019-05-31 14:48:52<br>
	 * 
	 * @param ownerUserId
	 * @param contactUserId
	 * @return boolean
	 * @version 1.1
	 */
	public boolean hasContact(Long ownerUserId, Long contactUserId) {
		ContactRelationVo bean = contactRelationDAO.getVOByContactUserId(ownerUserId, contactUserId);
		return bean != null;
	}
}
